# 计算两个rect对象之间的坐标差
import math


def two_rects(r1, r2):
    '''
    根据两个rect对象, 让r1追逐r2, 指定运动的方向
    :param r1: 追逐的rect
    :param r2:  被追逐的rect
    :return: (x向量,y向量)
    '''
    x1 = r1.x
    x2 = r2.x
    y1 = r1.y
    y2 = r2.y

    x = x2 - x1
    y = y2 - y1
    if x == 0 and y == 0:
        return 0, 0
    if x == 0 and y != 0:
        return 0, y / math.fabs(y)
    if x != 0 and y == 0:
        return x / math.fabs(x), 0
    return x / math.fabs(x), y / math.fabs(y)


if __name__ == '__main__':
    import pygame

    r1 = pygame.Rect(20, 39, 100, 100)
    r2 = pygame.Rect(30, 36, 100, 100)
    print(two_rects(r1, r2))
